//
//  BLToBeWashingViewController.m
//  blanket
//
//  Created by Mac on 2017/12/5.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "BLApplyUnionViewController.h"

@interface BLApplyUnionViewController ()
@property(nonatomic, strong) BLTableControl                     *nameControl;
@property(nonatomic, strong) BLTableControl                     *addControl;
@property(nonatomic, strong) BLTableControl                     *contactControl;
@property(nonatomic, strong) BLTableControl                     *phoneControl;
@property(nonatomic, strong) BLTableControl                     *signControl;
@property(nonatomic, strong) STButton                     *signButton;/**< 营业执照 */
@property(nonatomic, strong) NSString                     *url;/**< <##> */
@property(nonatomic, assign) BOOL                          didClic;/**<  */
@end

@implementation BLApplyUnionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self sendMyHoteleStatusRequest];

    // Do any additional setup after loading the view.
}
#pragma mark --subView
- (void)configSubView{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 122)];
    header.backgroundColor = self.tableView.backgroundColor;
    
    self.nameControl = [[BLTableControl alloc] initWithFrame:CGRectMake(15, 20 , UIScreenWidth - 30, 50) leftTitle:@"酒店名称" placeHolder:@"请输入酒店名称"];
    [header addSubview:self.nameControl];
    
    

    
    self.addControl = [[BLTableControl alloc] initWithFrame:CGRectMake(15, 20 + self.nameControl.bottom, UIScreenWidth - 30, 50) leftTitle:@"酒店地址" placeHolder:@"请输入酒店地址"];
    [header addSubview:self.addControl];
    
    
    self.contactControl = [[BLTableControl alloc] initWithFrame:CGRectMake(15, 20 + self.addControl.bottom, UIScreenWidth - 30, 50) leftTitle:@"联系人" placeHolder:@"请输入姓名"];
    [header addSubview:self.contactControl];
    

    self.phoneControl = [[BLTableControl alloc] initWithFrame:CGRectMake(15, 20 + self.contactControl.bottom, UIScreenWidth - 30, 50) leftTitle:@"联系电话" placeHolder:@"请输入电话号码"];
    self.phoneControl.textFiled.keyboardType = UIKeyboardTypeNumberPad;
    [header addSubview:self.phoneControl];
    
    self.signControl = [[BLTableControl alloc] initWithFrame:CGRectMake(15, 20 + self.phoneControl.bottom, UIScreenWidth - 30, 50) leftTitle:@"营业执照" placeHolder:@"请上传营业执照"];
    self.signControl.userInteractionEnabled = NO;
    self.signControl.textFiled.keyboardType = UIKeyboardTypeNumberPad;
    [header addSubview:self.signControl];
    

    self.signButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)
                                                     title:nil
                                                titleColor:nil
                                                 titleFont:0
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:[UIImage imageNamed:@"add"]
                                                     image:nil];
    self.signButton.centerY = self.signControl.centerY;
    self.signButton.left = UIScreenWidth / 2 + 45;
    [header addSubview:self.signButton];
    __weak typeof(self) weakSelf =  self;
    [self.signButton setClicAction:^(UIButton *sender) {
        [weakSelf showDefultImagePicker:^(UIImage *image) {
            [sender setImage:image forState:UIControlStateNormal];
            weakSelf.url = @"";
            weakSelf.didClic = YES;
            [TMUtils uploadImage:image handle:^(NSString *url) {
                weakSelf.url = url;
            }];
        }];
    }];
    

    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(20, self.signControl.bottom + 20, UIScreenWidth - 40, 20)
                                                     text:@"提示：请输入真实的酒店信息，以便我们联系您"
                                                textColor:BL_BlueBackGroundColor
                                                     font:12
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    [header addSubview:titleLable];
    
    header.height = titleLable.bottom + 20;
    self.tableView.tableHeaderView = header;
    
    
}
- (void)configTableFooter{
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = self.tableView.backgroundColor;
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, 0, UIScreenWidth - 60, 44)
                                                        title:@"提交"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:5
                                              backgroundColor:BL_BlueBackGroundColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedCommitButton];
    }];
    confimButton.st_centerY = 50;
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:confimButton];
    self.tableView.tableFooterView = footer;
}
- (void)configDidJoinIn{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, UIScreenHeight)];
    header.backgroundColor = self.tableView.backgroundColor;
    
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 60, 124, 95)];
    imageView.image = [UIImage imageNamed:@"预约成功"];
    imageView.centerX = UIScreenWidth / 2;
    [header addSubview:imageView];

    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, imageView.bottom, UIScreenWidth, 40)
                                                     text:@"已加入铺盖联盟"
                                                textColor:BL_BlueBackGroundColor
                                                     font:16
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    [header addSubview:titleLable];
    header.backgroundColor = UIColor.whiteColor;
    self.tableView.tableHeaderView = header;
}
#pragma mark --Action Method
- (void)onSelectedCommitButton{
    if (!self.contactControl.textFiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入联系人"];
        return;
    }
    if (!self.phoneControl.textFiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入联系电话"];
        return;
    }
//    if (!self.badControl.textFiled.text.length) {
//        [SVProgressHUD showErrorWithStatus:@"请输入酒店规模"];
//        return;
//    }
    if (!self.nameControl.textFiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入酒店名称"];
        return;
    }
    if (!self.addControl.textFiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入酒店地址"];
        return;
    }
    if (!self.didClic) {
        [SVProgressHUD showErrorWithStatus:@"请上传营业执照"];
        return;
    }
    if (!self.url.length) {
        [SVProgressHUD showInfoWithStatus:@"正在上传营业执照，请稍后"];
        return;
    }
    [self sendToBeUnionRequest];
}
- (void)sendToBeUnionRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"/Api/hotel/apply"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    
    if (self.nameControl.textFiled.text.length) {
        [dic setObject:self.nameControl.textFiled.text forKey:@"hotel_name"];
    }
    if (self.addControl.textFiled.text.length) {
        [dic setObject:self.addControl.textFiled.text forKey:@"address"];
    }
    
    
    if (self.contactControl.textFiled.text.length) {
        [dic setObject:self.contactControl.textFiled.text forKey:@"user_name"];
    }
    if (self.phoneControl.textFiled.text.length) {
        [dic setObject:self.phoneControl.textFiled.text forKey:@"phone"];
    }
    if (self.url.length) {
        [dic setObject:self.url  forKey:@"license"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"已经收到你的申请，请等待审核"];
                                                 [self.navigationController popViewControllerAnimated:YES];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
- (void)sendMyHoteleStatusRequest{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"/Api/hotel/user_card"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
//                                                 状态 1已加入 2审核未通过 3未提交申请
                                                 NSInteger data = [responseObject[@"data"] integerValue];
                                                 if (data == 1) {
                                                     [self configDidJoinIn];
                                                 }
                                                 if (data == 2) {
                                                     
                                                     [SVProgressHUD showInfoWithStatus:@"您的审核未通过，请重新上传您的申请"];
                                                     self.title = @"重新申请加入铺盖联盟";
                                                     [self configSubView];
                                                     [self configTableFooter];
                                                     [self.tableView st_autoAdjustAllResponder];
                                                 }
                                                 if (data == 3) {
                                                     self.title = @"加入铺盖联盟";
                                                     [self configSubView];
                                                     [self configTableFooter];
                                                     [self.tableView st_autoAdjustAllResponder];
                                                 }
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end
